
#include "project.h"


int main()
{
	ERR_load_crypto_strings();
	printf("%d\n", RAND_status());
	RSA *key = RSA_generate_key(1280, 17, NULL, NULL);
	printf("%d\n", RSA_size(key));
	if (key == NULL)
		ERR_print_errors_fp(stdout);
	else {
		int ret1 = i2d_RSAPublicKey(key, 0);
		unsigned char *Key, *Key1 = (unsigned char *)malloc(ret1);
		Key = Key1;
		int ret2 = i2d_RSAPublicKey(key, &Key1);
		if (ret1 != ret2){
			ERR_print_errors_fp(stdout);
			return 0;
		}
		printf("%d %p\n", ret1, Key);
		Key1 = Key;
		RSA *KEY = d2i_RSAPublicKey(NULL, (const unsigned char **)&Key1, ret2);
		if (KEY == NULL){
			ERR_print_errors_fp(stdout);
			return 0;
		}

		unsigned char MSG[1024];
		for (int i = 0; i < 768; i++)
			MSG[i] = (unsigned char)('A'+i%26);
		unsigned char buf[1024];
		printf("%d\n", RSA_size(key));
		int ret = RSA_public_encrypt(128-11, MSG, buf, key, RSA_PKCS1_PADDING);
		if (ret == -1)
			ERR_print_errors_fp(stdout);
		else {
			printf("%d\n", ret);
			unsigned char PLAINTEXT[1024];
			ret = RSA_private_decrypt(ret, buf, PLAINTEXT, key, RSA_PKCS1_PADDING);
			PLAINTEXT[ret] = 0;
			printf("%d %s\n", ret, PLAINTEXT);
		}
	}
}
